package city.spring.modules.material.controller;

import city.spring.modules.material.entity.SupplierEntity;
import city.spring.modules.material.service.SupplierService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController extends ApiController {
    /**
     * <strong>供应商管理</strong> Service
     */
    private final SupplierService supplierService;
    /**
     * 实体类（供应商管理）可排序字段
     */
    private final List<SFunction<SupplierEntity, ?>> entityOrderFields;

    public SupplierController(SupplierService supplierService) {
        this.supplierService = supplierService;
        this.entityOrderFields = new ArrayList<>();
        entityOrderFields.add(SupplierEntity::getId);
        entityOrderFields.add(SupplierEntity::getType);
        entityOrderFields.add(SupplierEntity::getAddress);
        entityOrderFields.add(SupplierEntity::getContact);
        entityOrderFields.add(SupplierEntity::getPhone);
        entityOrderFields.add(SupplierEntity::getStatus);
    }

    /**
     * 获取全部的 <strong>供应商管理</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, SupplierEntity entity) {
        LambdaQueryChainWrapper<SupplierEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>供应商管理</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "id", direction = Sort.Direction.DESC)}) Pageable pageable, SupplierEntity entity) {
        LambdaQueryChainWrapper<SupplierEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<SupplierEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>供应商管理</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(supplierService.getById(id));
    }

    /**
     * 添加一个 <strong>供应商管理</strong>
     *
     * @param entity 新增的信息
     */
    @PostMapping
    public Object add(@RequestBody SupplierEntity entity) {
        supplierService.save(entity);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>供应商管理</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody SupplierEntity entity) {
        supplierService.lambdaUpdate()
                .set(SupplierEntity::getId, entity.getId())
                .set(SupplierEntity::getSuppliername, entity.getSuppliername())
                .set(SupplierEntity::getType, entity.getType())
                .set(SupplierEntity::getContact, entity.getContact())
                .set(SupplierEntity::getPhone, entity.getPhone())
                .set(SupplierEntity::getAddress, entity.getAddress())
                .set(SupplierEntity::getStatus, entity.getStatus())
                .set(SupplierEntity::getDegree, entity.getDegree())
                .eq(SupplierEntity::getId, id)
                .update();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>供应商管理</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        supplierService.removeById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>供应商管理</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        supplierService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象： <strong>供应商管理</strong>
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<SupplierEntity> buildLambdaQuery(SupplierEntity entity) {
        LambdaQueryChainWrapper<SupplierEntity> lambdaQuery = supplierService.lambdaQuery();
        lambdaQuery.eq(entity.getId() != null, SupplierEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getSuppliername()), SupplierEntity::getSuppliername, entity.getSuppliername());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getContact()), SupplierEntity::getContact, entity.getContact());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getPhone()), SupplierEntity::getPhone, entity.getPhone());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getType()), SupplierEntity::getType, entity.getType());
        return lambdaQuery;
    }
}
